local element = ""
-- do it this way for proper grammar ;)
if game.player.faction == "air" or "earth" then
	element = "an " .. game.player.faction
elseif game.player.faction == "fire" or "water" then
	element = "a " .. game.player.faction
end

newChat{ id="welcome",
	text = "#WHITE#Ah! We finally get a visit from " .. element .. " elemental! I was wondering how long our barrier would hold.",
	answers = {
		{"Barrier? Is that why I had to gather the orbs to re-activate the portal here?", jump="talk"},
	}
}

newChat{ id="welcome1",
	text = "#WHITE#Ah! Welcome back @playername@! I see you got past our barrier again after the wizards managed to portal you out.",
	answers = {
		{"Barrier? Is that why I had to gather the orbs to re-activate the portal here?", jump="talk"},
	}
}

newChat{ id="talk",
	text = [[#WHITE#Yes @playername@, you see, when the wizards came, they saw this as an opportunity to increase their knowledge.  And they did...	However, at the same time, in their quest for knowlege, they opened up a gateway to the chaos realm, and it was then the quarrels started.  It has been long since chaos last was allowed to enter the world, and then we were hard pressed to defeat them.  In the end we had to resort to trickery and banishment rather than destruction.]],
	answers = {
		{"What do you mean?", jump="talk1"},
		{"What quarrels?", jump="quarrel"},
	}
}

newChat{ id="quarrel",
	text = [[#WHITE#Before the chaos gates were open, there were not open hostilities between the opposing elements, each knew its place... but when chaos came, they started thinking they were better than their opposing element, and indeed not much was needed to start open war.  Even the wizards became hostile toward each other, I believe chaos is manipulating them to achieve destruction and devestation, but they are sealed and banished.]],
	answers = {
		{"Banished? How?", jump="talk1"},
	}
}

newChat{ id="talk1",
	text = [[#WHITE#Well, @playername@, we had to seal them behind something, and we were the only beings strong enough to contain them, so we reversed their portal so that it would link to ours, in the process destroying all other gates to both their realm and ours.  Then in a final effort to keep them contained, we killed our portal from the inside, and only the orbs which you gathered had the strength to re-activate the portal. #LIGHT_GREEN#*At this he looks troubled*]],
	answers = {
		{"What troubles you?", jump="talk2"},
	}
}

newChat{ id="talk2",
	text = [[#LIGHT_GREEN#*He sighs*#WHITE# Now that you have reopened the portal here, I fear it will not be long before they break through, already I feel their power and yearning to escape again.]],
	answers = {
		{"That isn't good! Is there anything I can do, perhaps hide the orbs and de-activate the portal?", jump="talk3"},
	}
}

newChat{ id="talk3",
	text = [[#WHITE#No @playername@, that will not work, at best it would only delay their onset, I fear the time has come to take them head on and hopefully defeat them. Since you were brave, wise, and strong enough to solve the riddle on how to unlock the portal, I place upon you this charge.  Now go and defeat chaos and its minions and bring me their orb so that I can see if there is another way to contain them.  But be wary, opening our portal has allowed the wizards to summon mana elementals to aid them, and opening the portal to chaos will undoubtedly do the same.]],
	answers = {
	{"Errr... how do I open the portal to the chaos realm though?", jump="how"},
	{"Ok, I will see what I can do.", action=function(npc, player)
		player:grantQuest("destroy-chaos")
		game:setAllowedBuild("player_mana")
		end, },
	}
}

newChat{ id="how",
	text = [[#WHITE#That is simple, just reverse the opposite elements (fire and water, earth and air) and the portal to chaos will be opened.]],
	answers = {
	{"Thanks, I will see what I can do.", action=function(npc, player)
		player:grantQuest("destroy-chaos")
		game:setAllowedBuild("player_mana")
	end, }
	}
}

newChat{ id="quest",
	text = [[#WHITE#Well done @playername@!  The tides indeed have turned, and I think I have found a way to seal chaos again. Quick, bring me the Orb of Mana which is through the portal yonder. #LIGHT_GREEN#He points behind him.]],
	answers = {
	{"Ok!", action=function(npc, player)
		npc:move(34, 2, true)
	end}
	}
}

newChat{ id="quest1",
	text = [[#WHITE#Thank you @playername@!  #LIGHT_GREEN#As you hand him the orbs, they glow brightly, then a bright flash of light occurs as he presses them together, momentarily blinding you.  As you recover your sight, he speaks again. #WHITE#There! They once again are sealed behind us, hopefully this time it will take longer for them to get out.]],
	answers = {
	{"Ok!", action=function(npc, player)
		player:setQuestStatus("destroy-chaos", engine.Quest.DONE)
	end}
	}
}

newChat{ id="done",
	text = [[#WHITE#Ya mon!]],
	answers = {	{"Heh, I got some phat lewt adventuring, eh?"}	}
}

newChat{ id="waiting",
	text = [[#WHITE#I fear we do not have much time, you must act quickly and retrieve the Orb of Chaos for me, and defeat the chaos guardian in the way.]],
	answers = {
	{"Sorry, I will have it back post-haste."}
	}
}

if game.player:isQuestStatus("destroy-chaos", engine.Quest.COMPLETED) then
	return "quest"
elseif game.player:isQuestStatus("destroy-chaos", engine.Quest.DONE) then
	return "done"
elseif game.player:isQuestStatus("destroy-chaos", engine.Quest.PENDING) then
	return "waiting"
elseif game.player.faction == "mana" then
	return "welcome1"
--elseif game.player:findininventory("Orb of Chaos") and game.player:findininventory("Orb of Mana") then
--	return "quest1"
else
	return "welcome"
end
